package com.gujiangbo.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

@Configuration
public class JedisFactory {

    private static final String REDIS_HOST = "127.0.0.1";

    private static final int REDIS_PORT = 6379;

    /**
     * jedis 直接接
     *
     * @return
     */
    @Bean
    public Jedis jedis() {
        Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT);
        jedis.select(0);
        return jedis;
    }

    /**
     * JedisPool 连接池
     *
     * @return
     */
    @Bean
    public JedisPool jedisPool() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        //设置最大连接数为默认值的5倍
        poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL * 5);
        //设置最大空闲连接数为默认值的5倍
        poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 5);
        //设置开启JMX功能
        poolConfig.setJmxEnabled(true);
        //设置连接池没有连接后客户端的最大等待时间(单位为毫秒)
        // poolConfig.setMaxWaitMillis(300000000);
        JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_PORT);
        System.out.println(jedisPool);
        return jedisPool;
    }
}
